package org.uim.cricket.updater.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFileChooser;
import javax.swing.UIManager;

/**
 *
 * @author Isuru Madushanka
 */
public class frmUpdator extends javax.swing.JDialog {

	private static final long serialVersionUID = -8074703349566063234L;
	
	private String lastUpdateDate = null;
    private String repoDir = null;
    private boolean doContinue = true;

    /** Creates new form frmUpdator */
    public frmUpdator(java.awt.Frame parent, boolean modal, String lupdateDate, String repoDir) {
        super(parent, modal);

        this.lastUpdateDate = lupdateDate;
        this.repoDir = repoDir;
        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        initComponents();

        this.setCenterScreen();
        this.fillData();
    }

    private void fillData() {
        this.txtLastUpdated.setText(lastUpdateDate);
        this.txtRepoDir.setText(repoDir);
    }

    public String getLastUpdatedDate() {
        return this.txtLastUpdated.getText();
    }

    public String getRepositoryDir() {
        return this.txtRepoDir.getText();
    }

    public boolean isDoContinue() {
        return this.doContinue;
    }

    private void setCenterScreen() {
        Dimension window = this.getSize();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        int newX = (int) (screen.getWidth() - window.getWidth()) / 2;
        int newY = (int) (screen.getHeight() - window.getHeight()) / 2;

        this.setLocation(newX, newY);
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtLastUpdated = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtRepoDir = new javax.swing.JTextField();
        cmdBrowse = new javax.swing.JButton();
        cmdUpdate = new javax.swing.JButton();
        cmdCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cricket Updator");

        jLabel1.setText("Last Updated Date:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("(YYYY/MM/DD)");

        jLabel3.setText("Match Repository Directory:");

        cmdBrowse.setText("Browse...");
        cmdBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdBrowseActionPerformed(evt);
            }
        });

        cmdUpdate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdUpdate.setText("Update!");
        cmdUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdUpdateActionPerformed(evt);
            }
        });

        cmdCancel.setText("Cancel");
        cmdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmdUpdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdCancel))
                    .addComponent(cmdBrowse)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel3)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(txtLastUpdated, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(txtRepoDir)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtLastUpdated, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(28, 28, 28)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtRepoDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdBrowse)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdCancel)
                    .addComponent(cmdUpdate))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void cmdBrowseActionPerformed(java.awt.event.ActionEvent evt) {
        JFileChooser fchooser = new JFileChooser();
        fchooser.showDialog(this, "Select");
        String sel = fchooser.getSelectedFile().getParent();
        this.txtRepoDir.setText(sel);
    }

    private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {
        this.doContinue = false;
        this.setVisible(false);
    }

    private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) {
    	if (this.txtLastUpdated.getText() == null || this.txtLastUpdated.getText().length() == 0) return;
    	if (this.txtRepoDir.getText() == null || this.txtRepoDir.getText().length() == 0) return;
        this.doContinue = true;
        this.setVisible(false);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frmUpdator dialog = new frmUpdator(new javax.swing.JFrame(), true, null, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton cmdBrowse;
    private javax.swing.JButton cmdCancel;
    private javax.swing.JButton cmdUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField txtLastUpdated;
    private javax.swing.JTextField txtRepoDir;
    // End of variables declaration

}
